package com.atguigu.process.controller;

import com.atguigu.auth.service.SysUserService;
import com.atguigu.common.result.Result;
import com.atguigu.model.process.Process;
import com.atguigu.model.process.ProcessTemplate;
import com.atguigu.model.process.ProcessType;
import com.atguigu.process.service.ProcessService;
import com.atguigu.process.service.ProcessTemplateService;
import com.atguigu.process.service.ProcessTypeService;
import com.atguigu.vo.process.ApprovalVo;
import com.atguigu.vo.process.ProcessFormVo;
import com.atguigu.vo.process.ProcessQueryVo;
import com.atguigu.vo.process.ProcessVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags="审批相关接口")
@RestController
@RequestMapping("/admin/process")
@CrossOrigin    //支持跨域
public class ProcessController {
    @Autowired
    private ProcessService processService;

    @Autowired
    private ProcessTypeService processTypeService;

    @Autowired
    private ProcessTemplateService processTemplateService;

    @Autowired
    private SysUserService sysUserService;

    @ApiOperation("审批记录分页查询")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<ProcessVo>> pageQuery(@PathVariable Long page, @PathVariable Long limit,
                                              ProcessQueryVo processQueryVo){
        return processService.pageQuery(page,limit,processQueryVo);
    }

    @ApiOperation("查询所有的审批分类和每个分类对应的所有的审批模板")
    @GetMapping("/findProcessType")
    public Result<List<ProcessType>> findProcessType(){
        return Result.ok(processTypeService.findProcessTypeAndTemplate());
    }

    @ApiOperation("根据模板id获取模板信息")
    @GetMapping("/getProcessTemplate/{processTemplateId}")
    public Result<ProcessTemplate> getProcessTemplate(@PathVariable Long processTemplateId){
        return Result.ok(processTemplateService.getById(processTemplateId));
    }

    @ApiOperation("启动流程实例")
    @PostMapping("/startUp")
    public Result startUp(@RequestBody ProcessFormVo processFormVo){
        processService.startUp(processFormVo);
        return Result.ok();
    }

    @ApiOperation(value = "查询某人的待处理任务列表")
    @GetMapping("/findPending/{page}/{limit}")
    public Result<Page<ProcessVo>> findPending(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {
        return Result.ok(processService.findPengding(page,limit));
    }

    @ApiOperation("查看审批详情信息")
    @GetMapping("/show/{processId}")
    public Result<Map<String,Object>> show(@PathVariable Long processId){
        return Result.ok(processService.show(processId));
    }

    @ApiOperation(value = "审批")
    @PostMapping("approve")
    public Result approve(@RequestBody ApprovalVo approvalVo) {
        processService.approve(approvalVo);
        return Result.ok();
    }

    @ApiOperation(value = "已处理")
    @GetMapping("/findProcessed/{page}/{limit}")
    public Result<Page<ProcessVo>> findProcessed(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {
        return Result.ok(processService.findProcessed(page,limit));
    }

    @ApiOperation(value = "已发起")
    @GetMapping("/findStarted/{page}/{limit}")
    public Result<IPage<ProcessVo>> findStarted(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {
        Page<ProcessVo> pageParam = new Page<>(page, limit);
        return Result.ok(processService.findStarted(pageParam));
    }

    @ApiOperation(value = "获取当前用户基本信息")
    @GetMapping("getCurrentUser")
    public Result<Map<String,Object>> getCurrentUser() {
        return Result.ok(sysUserService.getCurrentUser());
    }
}
